package com.itheima.sc.impl.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.sms.SmsHomeAdvertiseApi;
import com.itheima.sc.mapper.sms.SmsHomeAdvertiseMapper;
import com.itheima.sc.pojo.sms.HomeAdvertise;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class SmsHomeAdvertiseImpl implements SmsHomeAdvertiseApi {

    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;

    //添加广告
    public void addAdvertise(HomeAdvertise advertise) {
        advertiseMapper.insert(advertise);
    }

    //单个或批量删除广告
    public void deleteAdvertise(List<Long> ids) {
        advertiseMapper.deleteBatchIds(ids);
    }

    //分页并根据条件查询广告列表
    public PageResult selectAdvertiseList(Integer page, Integer pageSize, Integer type, String name, String endTime) {
        //构建分页查询条件
        Page<HomeAdvertise> ipage = new Page<>(page, pageSize);
        //构建条件查询
        LambdaQueryWrapper<HomeAdvertise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(type), HomeAdvertise::getType, type);
        wrapper.eq(ObjectUtil.isNotEmpty(endTime), HomeAdvertise::getEndTime, endTime);
        wrapper.like(StrUtil.isNotBlank(name), HomeAdvertise::getName, name);
        wrapper.orderByDesc(HomeAdvertise::getSort);
        //查询请求
        ipage = advertiseMapper.selectPage(ipage, wrapper);
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }

    //根据对象来修改广告数据
    public void updateAdvertise(HomeAdvertise advertise) {
        advertiseMapper.updateById(advertise);
    }

    //查询广告详情
    public HomeAdvertise selectAdvertiseById(Long id) {
        return advertiseMapper.selectById(id);
    }

}
